package com.tju.serviceUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tju.JwtUtils;
import com.tju.MD5;
import com.tju.serviceHandler.GuliException;
import com.tju.serviceUcenter.entity.UcenterMember;

import com.tju.serviceUcenter.entity.VO.RegisterVO;
import com.tju.serviceUcenter.mapper.UcenterMemberMapper;
import com.tju.serviceUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author gsc
 * @since 2022-01-14
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember ucenterMember) {
        String phone = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member == null){
            throw new GuliException(20001,"用户名或密码错误!");
        }
        //密码不能用明文保存
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(20001,"用户名或密码错误!");
        }
        if (member.getIsDeleted()){
            throw new GuliException(20001,"该用户已被禁用!");
        }
        //登录成功，使用jwt来生成一个token
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;
    }



    //注册的方法
    @Override
    public void register(RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String code = registerVO.getCode();
        String nickname = registerVO.getNickname();
        String password = registerVO.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)
            || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败!");
        }
        //判断验证码是否正确
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误，注册失败");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count >= 1){
            throw new GuliException(20001,"该用户已注册！");
        }
        //添加到数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://gsc-oss.oss-cn-shanghai.aliyuncs.com/static_res/longkui3.jpg");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
